<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :title="actionStatus == 'edit' ? '编辑' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="20" :sm="24" :xs="24">
          <a-form-item label="班组名称" v-bind="validateInfos.name">
            <a-input
              allow-clear
              placeholder="请输入班组名称"
              v-model:value="form.name"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="门店用户" v-bind="validateInfos.user_ids">
            <a-select
              optionFilterProp="label"
              mode="multiple"
              :options="userData"
              allow-clear
              v-model:value="form.user_ids"
              placeholder="请选择门店用户"
              @change="change"
              autocomplete="off"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { get_user_list, detail } from '@/api/user/store_teams';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    editDataId: [String, Number],
    storeCode: String
  });

  const emit = defineEmits(['done', 'update:visible']);

  const userData = ref([]);

  onMounted(() => {
    form.store_code = props.storeCode;
    form.id = props.editDataId;
    let data = {
      page: 1,
      limit: 100,
      store_code: props.storeCode,
      is_show_leave: 0
    };
    get_user_list(data).then((res) => {
      res.list.forEach((item) => {
        userData.value.push({
          value: String(item.id),
          label: item.name
        });
      });
    });
    if (props.editDataId) {
      detail({ id: props.editDataId }).then((res) => {
        form.store_code = res.store_code;
        form.name = res.name;
        form.user_ids = res.user_ids.split(',');
      });
    }
  });

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const form = reactive({
    id: '',
    store_code: '',
    name: '',
    user_ids: []
  });
  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入班组名称',
        type: 'string',
        trigger: 'change'
      }
    ],
    user_ids: [
      {
        required: true,
        message: '请选择员工',
        type: 'array',
        trigger: 'change'
      }
    ]
  });

  const change = (e) => {
    console.log('e', e);
    form.user_ids = e;
  };

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    console.log('form:', form);
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
